/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package denver;

import java.util.ArrayList;
import net.lamot.java.jskype.general.AbstractMessenger;
import net.lamot.java.jskype.general.MessageListenerInterface;
import net.lamot.java.jskype.windows.Messenger;

/**
 *
 * @author Denis Lukyanov, skype:denver.fla
 */
public class Main implements MessageListenerInterface {

    private AbstractMessenger msgr = null;

    private ArgumentsParser argParser = null;
    private ArrayList<String> contacts = null;
    private ArrayList<String> chats = null;
    private ArrayList<String> messages = null;
    private ArrayList<String> chatIDs =null;

    private int readyTaskNum = 0;

    public Main(String[] args)
    {
        argParser = new ArgumentsParser();
        argParser.parse(args);

        if (!argParser.hasOption("message") || !(argParser.hasOption("contacts") || argParser.hasOption("chats")))
        {
            System.out.println("Incorrect arguments!");
            System.exit(1);
        }

        contacts = argParser.getValues("contacts");
        chats = argParser.getValues("chats");
        messages = argParser.getValues("message");

        if (
                (messages == null || messages.isEmpty()) ||
                (
                    (contacts == null || contacts.isEmpty()) &&
                    (chats == null || chats.isEmpty())
                )
           )
        {
            System.out.println("Empty message or contacts/chats!");
            System.exit(1);
        }

        readyTaskNum = 0;
        if (contacts != null)
        {
            readyTaskNum += contacts.size();
        }

        if (chats != null)
        {
            readyTaskNum += chats.size();
        }

        msgr = new Messenger();
        msgr.addListener(this);
        msgr.initialize();
        
        try
        {
            Thread.sleep(6000);
            //currentTask.invoke(this, null);
            sendMessages();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

    }

    public static void main(String[] args)
    {
        Main main = new Main(args);
    }

    @Deprecated
    private void createChats()
    {
        chatIDs = new ArrayList<String>();
        readyTaskNum = contacts.size();
        for (int i = 0; i < contacts.size(); i++)
        {
           msgr.sendMessage("CHAT CREATE "+ contacts.get(i));
        }
    }

    private void sendMessages()
    {
        System.out.println("sendMessages");
        if (contacts != null)
        {
            for (int i = 0; i < contacts.size(); i++)
            {
               msgr.sendMessage("MESSAGE "+ contacts.get(i) + " " + messages.get(0));
            }
        }

        if (chats != null)
        {
            for (int i = 0; i < chats.size(); i++)
            {
               msgr.sendMessage("CHATMESSAGE "+ chats.get(i) + " " + messages.get(0));
            }
        }
    }

    @Override
    public void onMessageReceived(String message) {
        //System.out.println(message);
        message = message.trim();

        if (validateTask(message))
        {
            readyTaskNum--;
        }

        if (readyTaskNum <= 0)
        {
           System.exit(1);
        }
    }

    private boolean validateTask(String message)
    {
        boolean retval = false;
        retval = message.startsWith("MESSAGE") && message.endsWith("STATUS SENDING");
        return retval;
    }

}
